import Pen.PenColor.IPenColor;
import Pen.PenColor.RedColor;
import Pen.PenModel.IPenModel;
import Pen.PenModel.MediumModel;
import Pen.PenType.PenTypeAbstract;
import Pen.PenType.PenTypePencil;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.lang.reflect.Constructor;

public class Main {
    //方法一：传统方法直接调用
    //    public static void main(String[] args) {
//
//
//
//        IPenColor penColor = new RedColor();
//        IPenModel penModel = new MediumModel();
//        PenTypeAbstract pen = new PenTypePencil(penColor,penModel);
//        pen.Pen();
//
//    }

    //方法二：读取xml
    public static void main(String[] args) {
        try {
            // 读取XML配置文件
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse("class_9_bridge//src//config.xml");
            Element root = doc.getDocumentElement();

            String penColorClassName = root.getElementsByTagName("PenColor").item(0).getTextContent();
            String penModelClassName = root.getElementsByTagName("PenModel").item(0).getTextContent();
            String penTypeClassName = root.getElementsByTagName("PenType").item(0).getTextContent();

            // 使用反射创建实例
            Class<?> penColorClass = Class.forName(penColorClassName);
            IPenColor penColor = (IPenColor) penColorClass.getDeclaredConstructor().newInstance();

            Class<?> penModelClass = Class.forName(penModelClassName);
            IPenModel penModel = (IPenModel) penModelClass.getDeclaredConstructor().newInstance();

            Class<?> penTypeClass = Class.forName(penTypeClassName);
            Constructor<?> penTypeConstructor = penTypeClass.getConstructor(IPenColor.class, IPenModel.class);
            PenTypeAbstract pen = (PenTypeAbstract) penTypeConstructor.newInstance(penColor, penModel);

            // 使用pen对象
            pen.Pen();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}